package com.chengn.paramtest;

import com.chengn.factory.MyThreadFactory;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class AliveTimeTest {
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(6,
                16,
                20, TimeUnit.SECONDS,
                new SynchronousQueue<>(),
                new MyThreadFactory());

        for (int i = 0; i < 10; ++i) {
            int j = i;
            TimeUnit.SECONDS.sleep(1);
            executor.execute(() -> {
                System.out.println(j + "  当前线程名：" + Thread.currentThread().getName());
                if (j < 6) {
                    try {
                        TimeUnit.HOURS.sleep(1);
                    } catch (Exception ex) {

                    }
                }
            });
        }

        TimeUnit.SECONDS.sleep(5);
        System.out.println("---------------------");



        for (int i = 0; i < 5; ++i) {
            int j = i;
            TimeUnit.SECONDS.sleep(3);
            executor.execute(() -> {
                System.out.println(j + "  当前线程名：" + Thread.currentThread().getName());
            });
        }

    }
}
